package services.midas.impl;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.google.gson.JsonObject;

import services.midas.CommandClassService;
import services.midas.MidasStubIntf;

import midas.util.DateUtils;
import models.common.user.DASSwBuild;
import models.common.user.DASUser;

import models.common.user.DASUserRole;
import models.midas.form.CommandClassForm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.core.MediaType;


public class CommandClassServiceImpl  extends MidasLocalStub implements CommandClassService {	
	
	@Override
	public boolean save(CommandClassForm commandClass) throws Exception {
		long editorId = 1L;
		JSONArray array = new JSONArray();
		array.put(commandClass.getJsonData());
		array.put(editorId);
		return super.getResponse("commandClass/save", array) == 200 ? true : false;
	}

	@Override
	public boolean accept(CommandClassForm commandClass) throws Exception {
		commandClass.setState("Accepted");
		commandClass.setAcceptedDate(DateUtils.now());
		commandClass.setComments("Accepted");
		return save(commandClass);
	}

	@Override
	public boolean decline(CommandClassForm commandClass) throws Exception {
		commandClass.setState("Draft");
		commandClass.setComments("Declined");
		return save(commandClass);
	}

	@Override
	public boolean propose(CommandClassForm commandClass) throws Exception {
		commandClass.setState("Proposed");
		commandClass.setComments("Proposed");
		return save(commandClass);
	}

	@Override
	public boolean cancel(CommandClassForm commandClass) throws Exception {
		commandClass.setState("Draft");
		commandClass.setComments("Canceled");
		return save(commandClass);
	}

	@Override
	public boolean checkGrammar(CommandClassForm commandClass) throws Exception {
		commandClass.setGrammar(true);
		commandClass.setComments("checked grammar");
		return save(commandClass);
	}

	@Override
	public CommandClassForm newVersion(CommandClassForm commandClass) throws Exception {
		commandClass.setId(0L);
		commandClass.setState("Draft");
		commandClass.setAcceptedDate("");
		commandClass.setGrammar(false);
		commandClass.setCreatedAt(DateUtils.now());
		commandClass.setComments("new version");
		return commandClass;
	}

	@Override
	public boolean delete(CommandClassForm commandClass) throws Exception {
		return false;
	}
	
	@Override	
	public boolean hasAvailableClassLetter() throws Exception {
		return this.getAvailableClassLetters().size() > 0;
	}

	@Override
	public List<DASSwBuild> getSwBuildList(String release, String build) {
		List<DASSwBuild> dasSwBuild = new ArrayList();
		dasSwBuild.add(new DASSwBuild(4,"SX","SX 1.0-1"));
		dasSwBuild.add(new DASSwBuild(5,"CX","CX 1.0-1"));
		dasSwBuild.add(new DASSwBuild(6,"CU","CU 1.0-2"));
		dasSwBuild.add(new DASSwBuild(7,"FX","FX 1.0-1"));
		dasSwBuild.add(new DASSwBuild(8,"BT","BT 1.0-2"));
		return dasSwBuild;
	}

	@Override
	public Map<Long,String> getAvailableClassLetters() throws Exception {
		JSONArray array = this.getJSONResponse("commandClass/availableLetters", JSONArray.class);
		Map<Long,String> result = new HashMap<Long,String>();
		for(int i = 0; i < array.length(); i++){
			JSONObject o = array.getJSONObject(i);
			result.put(o.getLong("id"), o.getString("letter"));
		}
		
		return result;
	}

	@Override
	public List<String> getAvailableVersions() throws Exception {
		List<String> availableVersions = new ArrayList<String>();
		availableVersions.add("1.2-0");
		availableVersions.add("1.3-0");
		return availableVersions;
	}

	@Override
	public CommandClassForm initCommandClass() throws Exception {
		CommandClassForm newMMC = new CommandClassForm();
		newMMC.setId(-1L);
		newMMC.setState("Draft");
		newMMC.setGrammar(false);
		newMMC.setCreatedAt(DateUtils.now());
		return newMMC;
	}
	
	@Override
	public JSONObject getCommandClassList(int pageIndex,int pageSize) {
		JSONObject result = new JSONObject();
		JSONArray mccList = this.getJSONResponse("commandClass/list/" + pageIndex + "/" + pageSize, JSONArray.class);
		try{
			JSONArray array = new JSONArray();
			for(int i = 1; i < mccList.length(); i++){
				JSONObject data = mccList.getJSONObject(i);
				JSONArray cells = new JSONArray();
				cells.put("<a href='/commandClass/" + data.getLong("id") + "'>" + data.getString("classLetter") + "</a>");
				cells.put(data.getString("menuText"));
				cells.put(data.getString("version"));
				cells.put(data.getString("state"));
				JSONObject json = new JSONObject();
				json.put("cell", cells);
				json.put("id", data.getLong("id"));
				array.put(json);
			}
			
			result.put("rows", array);
			result.put("page", pageIndex);
			result.put("records", pageSize);
			result.put("total", mccList.getInt(0));
			return result;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public JSONObject getCommandClassById(long id) {
		return this.getJSONResponse("commandClass/" + id, JSONObject.class);
	}
}
